package com.lemon.stock.util

import com.google.gson.Gson
import com.google.gson.JsonParser
import java.lang.Exception

object GsonUtils {
    val gson by lazy { Gson() }
    val parser by lazy { JsonParser() }

    fun toJson(src: Any): String{
        return gson.toJson(src)
    }

    inline fun <reified T> fromJson(json: String): T? {
        return fromJson(json, T::class.java)
    }

    fun <T> fromJson(json: String, clazz: Class<T>): T? {
        try {
            return gson.fromJson(json, clazz)
        }catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }
}